﻿using System;
using System.Data;

namespace GlobalStore.DataAccess.Transactioning
{
    public class TransactionScopeFactory : ITransactionScopeFactory
    {
        internal static readonly TimeSpan DefaultScopeTimeout = TimeSpan.FromMinutes(1);

        private static TimeSpan TransactionTimeout
        {
            get
            {
                //TimeSpan timeout;
                //if (!TimeSpan.TryParse(ConfigurationManager.AppSettings["PdpTransactionScopeTimeout"], out timeout))
                    return DefaultScopeTimeout;

                //return timeout;
            }
        }

        private static IsolationLevel DefaultIsolationLevel
        {
            get
            {
                //return ConfigurationManager.AppSettings["PdpTransactionScopeDefaultIsolationLevel"].ParseAsEnumSafe(IsolationLevel.ReadCommitted);
                return IsolationLevel.ReadCommitted;
            }
        }

        private static IsolationLevel HighIsolationLevel
        {
            get
            {
                //return ConfigurationManager.AppSettings["PdpTransactionScopeHighIsolationLevel"].ParseAsEnumSafe(IsolationLevel.Serializable);
                return IsolationLevel.Serializable;
            }
        }

        public ITransactionScope Create()
        {
            return new NHibernateReadWriteTransactionScope(DefaultIsolationLevel, TransactionTimeout);
        }

        public ITransactionScope CreateReadOnly()
        {
            return new NHibernateReadOnlyTransactionScope(DefaultIsolationLevel, TransactionTimeout);
        }

        public ITransactionScope CreateIsolated()
        {
            return new NHibernateReadWriteTransactionScope(HighIsolationLevel, TransactionTimeout);
        }

        public ITransactionScope CreateDataBaseRecreator()
        {
            return new NHibernateReadWriteTransactionScope(true, HighIsolationLevel, TransactionTimeout);
        }
    }
}